<template>
  <el-container class="home-container">
    <!--头部区域-->
    <el-header>
      <div>
        <img src="../assets/logo.png" alt="">
        <span>用户流量行为监测系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!--页面主题区域-->
    <el-container>
      <!--侧边栏-->
      <el-aside :width="isCollapse ? '64px': '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!--侧边栏菜单区域-->
        <el-menu
                :default-active="activePath"
                :router="true"
                background-color="#333744"
                text-color="#fff"
                active-text-color="#409EFF"
                :collapse="isCollapse"
                :collapse-transition="false">
          <!--index跳转地址-->
          <el-menu-item :index="item.path" v-for="item in menulist" :key="item.id" @click="saveNavState(item.path)">
            <!--图标-->
            <i :class="iconsObj[item.id]"></i>
            <!--文本-->
            <span>{{item.authName}}</span>
          </el-menu-item>

        </el-menu>
      </el-aside>
      <!--右侧内容主体-->
      <el-main>
        <!--路由占位符-->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
// @ is an alias to /src
export default {
  name: 'Home',
  data () {
    return {
      // 左侧菜单栏
      menulist: [
        {
          authName: '概述界面',
          id: 1,
          path: '/Overview'
        },
        {
          authName: '用户详情页',
          id: 2,
          path: '/UserDetailsTable'
        },
        {
          authName: '用户流量表',
          id: 3,
          path: '/FlowLimitTable'
        },
        {
          authName: '管理员表',
          id: 4,
          path: '/AdminTable'
        },
        {
          authName: '用户规则表',
          id: 5,
          path: '/UserRuleTable'
        },
        {
          authName: '访问类型表',
          id: 6,
          path: '/AccessTypeTable'
        },
        {
          authName: '日志表',
          id: 7,
          path: '/Log'
        }
      ],
      // 图标对象，菜单的id作为键，图标作为值
      iconsObj: {
        '1': 'iconfont icon-baobiao',
        '2': 'iconfont icon-tijikongjian',
        '3': 'iconfont icon-danju',
        '4': 'iconfont icon-dengluyonghuming',
        '5': 'iconfont icon-danju',
        '6': 'iconfont icon-icon_users',
        '7': 'iconfont icon-danju'
      },
      // 是否折叠
      isCollapse: false,
      // 被激活的链接地址
      activePath: ''
    }
  },
  created () {
    // 整个home组件创建的时候就 从sessionStorage把值取出来付给activePath进行高亮
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 点击按钮切换菜单的折叠展开
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    // 保存链接的激活状态
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}

.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  // div中的元素垂直居中
  div {
    display: flex;
    align-items: center;

    img {
      width: 50px;
      height: 50px;
      border-radius: 50%;
    }

    span {
      margin-left: 15px;
    }
  }
}

.el-aside {
  background-color: #333744;

  .el-menu {
    border-right: none;
  }
}

.el-main {
  background-color: #EAEDF1;
}

.iconfont {
  margin-right: 10px;
}

.toggle-button {
  background-color: #4A5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;

}
</style>
